//
//  HomePage-aboutView.swift
//  Uber
//
//  Created by 朱 桓 on 16/1/24.
//  Copyright © 2016年 zhu.huan. All rights reserved.
//

import UIKit

extension HomePage {

    /**
    *初始化左边区域
    */
    func addLeftView()
    {
        if leftView == nil {
            var frame:CGRect! = self.navigationController?.view.bounds
            
            frame.origin.x = -frame.size.width+PanWidth
            frame.size.width -= PanWidth
            // 当前leftView的坐标: leftView.frame = (-220,0,220,568)
            leftView = UIView(frame: frame)
            leftView?.backgroundColor = UIColor.blackColor()
            self.navigationController!.view.addSubview(leftView!)
        }
    }
    
    /**
    *初始化右边区域
    */
    func addBackControl()
    {
        if backControl == nil {
            let frame:CGRect = self.navigationController!.view.bounds
            // backControl.frame = (0,0,320,568)
            backControl = UIControl(frame: frame)
            backControl.backgroundColor = UIColor.blackColor()
            // 设置透明度
            backControl.alpha = 0.5
            backControl.addTarget(self, action: Selector("doTapCenter:"), forControlEvents: UIControlEvents.TouchUpInside)
            
            self.navigationController!.view.addSubview(backControl)
            self.navigationController!.view.sendSubviewToBack(backControl)
        }
    }
    
    /**
    *显示设置界面
    */
    func addUserCenterView()
    {
        userPage = UserCenterPage()
        userPage.view.frame = leftView!.bounds
        userPage.owner = self
        
        leftView?.addSubview(userPage.view)
    }
    
    /**
    *跳转设置界面
    */
    func doShowSettingPage()
    {
        showCenterPanel()
        self.navigationController!.view.sendSubviewToBack(self.backControl)
        
        let page = SettingPage()
        let navPage = UINavigationController(rootViewController: page)
        
        self.presentViewController(navPage, animated: true, completion: nil)
    }
    
    /**
    *显示左边视图
    */
    func showLeftPanel()
    {
        showingLeft = true
        showCenterView(leftView!, offset: -2, shadow: true)
        
        // 添加动画
        UIView.animateWithDuration(1.0, delay: 0, options:.BeginFromCurrentState, animations: { () -> Void in
            var frame = self.leftView?.frame
            frame?.origin.x = 0
            // leftView.frame = (0,0,220,568)
            self.leftView?.frame = frame!
            
            frame = self.navigationController?.navigationBar.frame
            frame?.origin.x = (frame?.size.width)! - PanWidth
            self.navigationController?.navigationBar.frame = frame!
            }, completion: { (finished:Bool) in
                if finished {
                    // 右边和右边区域都显示在前面
                    self.navigationController!.view.bringSubviewToFront(self.backControl)
                    self.navigationController!.view.bringSubviewToFront(self.leftView!)
                }
        })
    }
    
    /**
    *显示右边中心视图
    */
    func showCenterPanel()
    {
        showingLeft = false
        
        UIView.animateWithDuration(1.0, delay: 0, options:UIViewAnimationOptions.BeginFromCurrentState, animations: { () -> Void in
            var frame = self.leftView?.frame
            print("frame:\(frame)")
            frame?.origin.x = -frame!.size.width
            self.leftView?.frame = frame!
            
            frame = self.navigationController!.navigationBar.frame
            frame?.origin.x = 0
            self.navigationController!.navigationBar.frame = frame!
            }) { (finished) -> Void in
                if finished {
                    // 右边控制区隐藏
                    self.navigationController!.view.sendSubviewToBack(self.backControl)
                    self.showCenterView(self.leftView!, offset: 0, shadow: false)
                }
        }
    }
    
    /**
    *设置视图的属性
    *
    *@param view 源视图
    *@param affset 偏移位置
    *@param shadow 是否有阴影
    */
    func showCenterView(view:UIView, offset:CGFloat, shadow:Bool)
    {
        if shadow {
            view.layer.shadowColor = UIColor.blackColor().CGColor
            view.layer.shadowOpacity = 0.2
            view.layer.shadowOffset = CGSizeMake(offset, offset)
        } else {
            // 没有圆角
            view.layer.cornerRadius = 0.0
            view.layer.shadowOpacity = 0.0
            view.layer.shadowOffset = CGSizeMake(offset, offset)
        }
    }
    
    /**
    *中心区域点击事件
    */
    func doTapCenter(sender: AnyObject)
    {
        if showingLeft! {
            showCenterPanel()
        }
    }
    
    /**
    *导航栏点击事件
    */
    func doShowLeft()
    {
        if showingLeft! {
            showCenterPanel()
        } else {
            showLeftPanel()
        }
    }
    
    /**
    *点击乘车的类型(人民优步/优选轿车/七座商务轿车/高级轿车/)
    */
    @IBAction func doSwapCar(sender: UIButton)
    {
        // 设置当前点击按钮图片样式
        btnHelper!.normalImageName = ""
        btnHelper!.selectedImageName = "CarBtn.png"
        btnHelper!.setBottonEx(sender)
        
        // 前一个label位置往下移动
        var rt:CGRect = preLabel.frame;
        rt.origin.y += moveOffset
        preLabel.frame = rt
        
        // 当前按钮对应的label位置往上移动
        let labelTag = String(format: "1%d", sender.tag)
        let currLabel = self.view.viewWithTag(Int(labelTag)!) as! UILabel
        var frame:CGRect = currLabel.frame
        frame.origin.y -= moveOffset
        currLabel.frame = frame
        // 当前的prelabel赋值
        preLabel = currLabel
    }
    
    /**
    *给bottomView添加一个滑动手势
    */
    func addTapGuestrue()
    {
        let tap = UIPanGestureRecognizer(target: self, action: "doPanGuestrue")
        bottomView?.addGestureRecognizer(tap)
    }
    
    func doPanGuestrue()
    {
        isMoveup = !isMoveup
        self.animationView(isMoveup)
    }
    
    /**
    *滑动显示的动画
    *@param moveUp true: 往上移动 false:往下移动
    */
    func animationView(moveUp:Bool)
    {
        UIView.animateWithDuration(0.5) { () -> Void in
            var frame = self.bottomView!.frame
            if moveUp {
                frame.origin.y = self.view.bounds.size.height-frame.size.height
            } else {
                frame.origin.y = self.view.bounds.size.height-80
            }
            self.bottomView!.frame = frame
        }
    }
    
    /**
    *获得估算费用
    */
    @IBAction func doGetgusuan(sender:UIButton)
    {
        sender.hidden = true
        self.bottomImageView.image = UIImage(named: "FeiYongBack.png")
        endPos.text = "北京南站"
        FeiYong.text = "￥69-87"
    }
    
}
